<template>
  <div class="resume_header">
    <span>已申请的岗位</span>
  </div>
  <div class="resume_center">

    <el-table :data="filterTableData.slice((page - 1) * limit, page * limit)" style="width: 100%" height="520px"
      size="large" @filter-change="tablesort">

      <el-table-column label="岗位" prop="jobName" sortable width="240" column-key="jobIndustry"
        :filters="filterJobNameData">
        <!-- <el-table-column label="岗位" prop="jobName" sortable width="240" column-key="jobIndustry"
        :filters="filterJobNameData" :filter-method="filterJob"> -->
        <template #default="scope">
          <router-link :to="{ path: '/postionInfoPage', query: { jobId: scope.row.jobId } }">
            {{ scope.row.jobName }}
          </router-link>
        </template>

      </el-table-column>
      <el-table-column label="公司" prop="eName" sortable width="220" column-key="eName" :filters="filterENameData">
        <!-- <el-table-column label="公司" prop="eName" sortable width="220" column-key="eName" :filters="filterENameData"
        :filter-method="filterEnterprise"> -->
        <template #default="scope">
          <router-link :to="{ path: '/eInfoPage', query: { eId: scope.row.eId } }">
            {{ scope.row.eName }}
          </router-link>
        </template>
      </el-table-column>

      <el-table-column align="right">
        <template #header>
          <el-input v-model="search" size="default" placeholder="搜索" />
        </template>
        <template #default="scope">
          <el-popconfirm width="220" confirm-button-text="是" cancel-button-text="否" icon-color="#626AEF" title="是否确认取消?"
            cancel-button-type="info" @confirm="cancelCollection(scope.$index, scope.row)">
            <template #reference>
              <!-- <el-button size="default" type="danger" @click="cancelCollection(scope.$index, scope.row)">取消收藏</el-button> -->
              <el-button size="default" type="danger">取消收藏</el-button>
            </template>
          </el-popconfirm>


        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <Pagination :page="page" :limit="limit" :handleSizeChange="handleSizeChange"
        :handleCurrentChange="handleCurrentChange" :filterTableData="filterTableData" :pagesizes="pagesizes" />

    </div>

  </div>
</template>
<script lang='ts' setup>
import { TableColumnCtx } from 'element-plus/es/components/table/src/table-column/defaults';
import { data, elmessage } from "@/hook/hook"
import { getUserCollectionJobs, upUserCollectionJob } from "@/api/userApi"
import { computed, ref, onMounted, reactive, watch, onBeforeMount } from 'vue'
import { useStore } from "vuex"
import { useRouter } from "vue-router"
const store = useStore()
const router = useRouter()
const user = store.state.userState
// 表格数据定义
interface CollectionJob {
  jobId: string
  jobName: string
  jobIndustry: string
  eId: string
  eName: string
}
const tableData: CollectionJob[] = reactive([])

const search = ref('')
const filterTableData = computed(() =>
  tableData.filter(
    (data) =>
      !search.value ||
      // 通过岗位名称筛选
      // data.name.toLowerCase().includes(search.value.toLowerCase())
      data.jobName.toLowerCase().indexOf(search.value.toLowerCase()) !== -1
  )
)
// 定义筛选类型
interface filterType {
  text: string,
  value: string
}
var jobIndustryArr:any=reactive([])
var eNameArr:any=reactive([])
const tableData_1: CollectionJob[]=reactive([])
const setJobIndustryArr=(arr:any)=>{
  jobIndustryArr=arr
}
const getJobIndustry=()=>{
  return jobIndustryArr
}
const setENameArr=(arr:any)=>{
  eNameArr=arr
}
const getENameArr=()=>{
  return eNameArr
}

const tablesort = (filterArr: CollectionJob) => {

  if ((typeof (filterArr.jobIndustry)) == "object") {
    setJobIndustryArr(filterArr.jobIndustry)
  }
  if ((typeof (filterArr.eName)) == "object") {
    setENameArr(filterArr.eName)
  }


  // 清除原表格数据
  tableData.splice(0, tableData.length)
  
  const jobIndustryLength = getJobIndustry().length
  const eNameLength = getENameArr().length

  // 按筛选条件重新加入数据
  tableData_1.forEach((valu: any, index: any, arr_1: any) => {
    // 所有筛选重置，即最初排序
    if (jobIndustryLength == 0 && eNameLength == 0) {
      if (tableData.findIndex((item: { jobId: string; }) => item.jobId == valu["jobId"]) == -1) {
        tableData.push(valu)
      }

    }

    // 只筛选岗位行业
    else if (jobIndustryLength > 0 && eNameLength == 0) {
      if (getJobIndustry().includes(valu["jobIndustry"])) {
        if (tableData.findIndex((item: { jobId: string; }) => item.jobId == valu["jobId"]) == -1) {
          tableData.push(valu)
        }
      }
    }
    // 只筛选公司名称
    else if (jobIndustryLength == 0 && eNameLength > 0) {
      if (getENameArr().includes(valu["eName"])) {
        if (tableData.findIndex((item: { jobId: string; }) => item.jobId == valu["jobId"]) == -1) {
          tableData.push(valu)
        }
      }
    }

    // 筛选岗位行业、公司名称
    else if (jobIndustryLength > 0 && eNameLength > 0) {
      if (getJobIndustry().includes(valu["jobIndustry"]) && getENameArr().includes(valu["eName"])) {
        if (tableData.findIndex((item: { jobId: string; }) => item.jobId == valu["jobId"]) == -1) {
          tableData.push(valu)
        }

      }
    }

  })

}
// // 岗位名称筛选排序
// const filterJob = (
//   value: string,
//   row: CollectionJob,
//   column: TableColumnCtx<CollectionJob>
// ) => {
//   // const property = column['property']
//   return row["jobIndustry"] === value
// }

const filterJobNameData: filterType[] = reactive([])

// // 公司名称筛选排序
// const filterEnterprise = (
//   value: string,
//   row: CollectionJob,
//   column: TableColumnCtx<CollectionJob>
// ) => {
//   // const property = column['property']
//   return row["eName"] === value
// }

const filterENameData: filterType[] = reactive([])

// 取消收藏岗位
const cancelCollection = (index: number, row: CollectionJob) => {
  // store.commit("upUserCollectionJobs", row)
  const newData = {
    ...data(),
    jobId: row.jobId
  }
  upUserCollectionJob(newData).then((res: any) => {
    if (res.data["code"] == "200") {
      elmessage(res.data["message"], res.data["type"])
      tableData.splice(index, 1)
    } else if (res.data["code"] == "300") {
      elmessage(res.data["message"], res.data["type"])
      setTimeout(() => {
        router.replace("/login")
      }, 500);
      localStorage.clear()
    } else {
      console.log(res.data["message"]);

    }
  })

}



// 分页器
const page = ref(1)
const pagesizes = ref([1, 2, 3, 4, 10, 20, 30, 40])
const limit = ref(10)

const handleSizeChange = (val: number) => {
  limit.value = val
  page.value = 1

}
const handleCurrentChange = (val: number) => {
  page.value = val
}

const init = async () => {
  await getUserCollectionJobs(data()).then((res: any) => {
    if (res.data["code"] == "200") {
      res.data.datas.forEach((value: any, index: any, self: any) => {
        tableData.push(value)
        tableData_1.push(value)
      })
      res.data["industrys"].forEach((value: any, index: any, self: any) => {
        filterJobNameData.push(value)
      });
      res.data["es"].forEach((value: any, index: any, self: any) => {
        filterENameData.push(value)
      });
    } else if (res.data["code"] == "300") {
      elmessage(res.data["message"], res.data["type"])
      setTimeout(() => {
        router.replace("/login")
      }, 500);
      localStorage.clear()
    } else {
      console.log(res.data["message"]);

    }
  })
}


onBeforeMount(async () => {
  
  await init()
  // pushTableData()

})


</script>
<style scoped>
.pagination {
  float: right;
  margin-right: 50px;
  /* right: 150px; */
}


.resume_center {
  font-family: '微软雅黑';
  height: 800px;
  background-color: #FFF;
}

.resume_header {
  background-color: rgb(215, 215, 215);
  height: 50px;
  display: flex;
  align-items: center;
  padding-left: 20px;

}

a {
  text-decoration: none;
  color: #000;
}
</style>